package com.wanbangee.marksystem.service.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wanbangee.marksystem.dao.UAccountDao;
import com.wanbangee.marksystem.dao.UClassSubjectRelationDao;
import com.wanbangee.marksystem.dao.UTeacherDao;
import com.wanbangee.marksystem.entities.UAccount;
import com.wanbangee.marksystem.entities.UTeacher;
import com.wanbangee.marksystem.service.UTeacherService;

@Service
public class UTeacherServiceImp implements UTeacherService {

	@Autowired
	private UTeacherDao uTeacherDao;
	@Autowired
	private UAccountDao uAccountDao;
	@Autowired
	private UClassSubjectRelationDao uClaSubRelationDao;
	
	@Override
	@Transactional
	public Integer addTeacher(UTeacher uteacher) {
		// TODO Auto-generated method stub
		//1 插入账户表
		UAccount uaccount = new UAccount();
		uaccount.setAddTime(uteacher.getAddTime());
		uaccount.setAddUserId(uteacher.getAddUserId());
		uaccount.setRole(1);
		uaccount.setLoginName(uteacher.getTeaMobile());
		uaccount.setLoginPass(uteacher.getTeaMobile());
		uaccount.setSchId(uteacher.getSchId());
		uaccount.setSchNm(uteacher.getSchNm());
		uAccountDao.insertTeacherAccount(uaccount);
		//2 插入教师表
		uteacher.setAccId(uaccount.getAccId());
		uteacher.setTeaState(1);
		//执行插入
		uTeacherDao.addTeacher(uteacher);
		
		return uteacher.getTeaId();

	}

	@Override
	@Transactional
	public Integer deleteTeacher(Integer teaId) {
		System.out.println(teaId);
		if (uClaSubRelationDao.selectTeacherCountByTeaId(teaId)>0) {
			return 10013;
		} else {
			int accId = uTeacherDao.selectAccIdByTeaId(teaId);
			uAccountDao.deleteTeacherAccountByAccId(accId);
			uTeacherDao.updateTeacherStateByTeaId(teaId);
		}
		return 1001;
	}

	@Override
	public Integer updateTeacher(UTeacher uteacher) {
		uTeacherDao.updateTeacher(uteacher);
		return 1001;
	}

	@Override
	public UTeacher selectTeacherByTeaId(Integer teaId) {
		// TODO Auto-generated method stub
		return this.uTeacherDao.selectTeacherByTeaId(teaId);
	}

	@Override
	public List<UTeacher> selectTeacherBySchIdAndSubId(Integer schId, Integer subId, Integer pageNum,
			Integer pageSize) {
		// TODO Auto-generated method stub
		pageNum = (pageNum-1)*pageSize;
		return this.uTeacherDao.selectTeacherBySchIdAndSubId(schId, subId, pageNum, pageSize);
	}

	@Override
	public UTeacher selectTeacherByaccId(Integer accId) {
		// TODO Auto-generated method stub
		return this.uTeacherDao.selectTeacherByaccId(accId);
	}

	@Override
	public List<UTeacher> selectTeacherBySchId(Integer schId, Integer pageNum, Integer pageSize) {
		
		int abc = (pageNum-1)*pageSize;
		return this.uTeacherDao.selectTeacherBySchId(schId,abc,pageSize);
	}

	@Override
	public Integer selectCountTeacherBySchId(Integer schId) {
		return this.uTeacherDao.selectCountTeacherBySchId(schId);
	}

	@Override
	public List<UTeacher> selctTeacherByTeaNmAndSchId(String teaNm, Integer schId) {

		String teaName = "%"+teaNm+"%";
		return this.uTeacherDao.selctTeacherByTeaNmAndSchId(teaName, schId);
	}

	
}

